package com.affectu.common.util;

import java.sql.Connection;
import java.sql.DriverManager;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;

import com.affectu.common.base.UtilBase;
import com.affectu.user.User;

/**
 * 2008-8-10
 * 
 * @author daniel nathan
 * 
 */
public class PersistanceUtil extends UtilBase {

	private static PersistanceUtil instance = new PersistanceUtil();

	private static SessionFactory sessionFactory = new AnnotationConfiguration()
			.configure().buildSessionFactory();

	private PersistanceUtil() {
	}

	public static PersistanceUtil getInstance() {
		return instance;
	}

	public FullTextSession getFullTextSession() {
		return Search.createFullTextSession(this.getSession());
	}

	public Session getSession() {
		return sessionFactory.openSession();
	}

	public Connection getConnection() {
		Connection con = null;
		try {
			// System.out.println("����Web Containerl�ӳؿ�ʼ...");
			/*
			 * InitialContext ctx = new InitialContext(); DataSource ds =
			 * (DataSource) ctx.lookup("jdbc/affectu"); con =
			 * ds.getConnection();
			 */

			Class.forName("org.gjt.mm.mysql.Driver").newInstance(); // ����Class.
			// forName(
			// "com.mysql.jdbc.Driver"
			// );
			String url = "jdbc:mysql://localhost/sq_affectu?user=sq_affectu&password=c5q7t4&useUnicode=true&characterEncoding=UTF-8";
			// myDBΪ��ݿ���
			con = DriverManager.getConnection(url);

		} catch (Exception ex) {
			ex.printStackTrace(System.out);
		}
		return con;
	}

	// @WebRemote
	public User getUser(String name) {
		User user = new User();
		user.setTrueName(name);
		user.setGender("0/1");
		return user;
	}
}
